package com.i2863.tools.weixinmp.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

/**
 * Created by xuweiwei on 2017/7/31.
 */
public class QiniuUpload {
    private String ak;
    private String sk;
    private String bucketName;
    private String prefix;
    private Auth auth;
    public  UploadManager uploadManager;
    private static final Logger logger = LoggerFactory.getLogger(QiniuUpload.class);

    public QiniuUpload(String ak, String sk, String bucketName, String host) {
        this.ak = ak;
        this.sk = sk;
        this.bucketName = bucketName;
        this.prefix = "http://"+host+"/";
        auth = Auth.create(ak, sk);
        Zone z = Zone.autoZone();
        Configuration c = new Configuration(z);
        uploadManager = new UploadManager(c);
    }


    /**
     *  获取覆盖上传token
     * @param bucketName
     * @param key
     * @return
     */
    public String getUpToken(String bucketName, String key) {
        return auth.uploadToken(bucketName,key);
    }


    public String upload(String key, byte[] bs) throws IOException {
        try {
            //调用put方法上传
            Response res = uploadManager.put(bs, key, getUpToken(bucketName,key));
            //打印返回的信息
            String body = res.bodyString();
            JSONObject object = JSON.parseObject(body);
            String key1 = object.getString("key");
            if (key1 == null || key1.length() <= 0) {
                throw new RuntimeException("无效的key:" + key1);
            }
            return   prefix + key1;
        } catch (QiniuException e) {
            Response r = e.response;
            String rbody = "";
            try {
                //响应的文本信息
                rbody = r.bodyString();
            } catch (QiniuException e1) {
                rbody = r.toString();
            }
            logger.error("上传用户头像异常,响应内容:{}", rbody, e);
            throw new RuntimeException(e);
        }
    }


    public static void main(String[] args) throws IOException {
        QiniuUpload qiniuUpload = new QiniuUpload("LplD_2KC-o7-3omKyCTgXjnHgPUITUFm6F0ck-T0","ENZvx9UweRe8ekH1po7ffovDiapDqZS8UltW9wal","images-bucket","imageccsobey.i2863.com");
        System.out.println(qiniuUpload.upload("123.txt","123456".getBytes()));

    }

}
